package com.gaojiawei.multiThread03_ThreadNormalMethod;

import org.junit.Test;

/**
 * 线程吃的常用方法
 * 获取当前线程
 * 设置线程的名字
 * 设置线程的优先级
 * 线程的让步
 * 线程的休眠
 * 线程的抢占
 * 守护线程
 * 线程的等待与唤醒
 */
public class ThreadNormalMethod {

    /**
     * 获取当前线程
     */
    @Test
    public void testGetThread(){
        Thread thread = Thread.currentThread();
        System.out.println(thread);
    }

    /**
     * 在构建Thread的时候，一定要设置一个有意义的线程名，方便后期排查问题
     * */
    @Test
    public void testSetName(){
        Thread thread = new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
        });
        thread.setName("模块名：功能名：i");
        thread.start();
    }

    /**
     * 设置线程的优先级
     */
    @Test
    public void testSetPriority(){
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("t1:" + i);
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("t2:" + i);
            }
        });

        t1.setPriority(1);
        t2.setPriority(10);
        t1.start();
        t2.start();


    }

    /**
     * 线程的让步
     */
    @Test
    public void testYelid(){
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("t1:" + i);
                if(i == 50){
                    //线程的让步，Thread.yelid()方法不会释放锁，只是释放cpu的时间片
                    Thread.yield();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("t2:" + i);
            }
        });

        t1.setPriority(1);
        t2.setPriority(10);
        t1.start();
        t2.start();
    }


    /**
     * 线程的休眠
     */
    @Test
    public void testSleep() throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(10000);
                System.out.println("sleep 结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });


        thread.start();
        Thread.sleep(20000);
    }

    /**
     * join t1.join()t1必须执行完main才可以继续执行，t1.join(1000)在这一秒内先执行t1线程
     * @throws InterruptedException
     */
    @Test
    public void testJoin() throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(i);
            }
        });

        thread.start();

        for(int i = 0;i< 100;i++){
            System.out.println("main"+i);
            if(i == 50){
                thread.join();
            }
        }
    }

    /**
     *
     *守护线程
     */
    @Test
    public void testDaemon(){
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    System.out.println(i);
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread1.setDaemon(true);
        thread1.start();

    }

    /**
     * 线程的等待和唤醒
     * @throws InterruptedException
     */
    @Test
    public void testWatiAndNotify() throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread1 = new Thread(() -> {
            try {
                sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread.start();
        thread1.start();
        Thread.sleep(10000);
        synchronized (ThreadNormalMethod.class){
//            Thread.sleep(10000);
            ThreadNormalMethod.class.notifyAll();
        }

    }


    public static synchronized void sync() throws InterruptedException {
        for(int i =0;i<10;i++){
            if(i == 5){

                ThreadNormalMethod.class.wait();
            }
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+i);
        }
    }









}
